//
//  dBCpblJLCFMineAddressAddViewControllerjrpfOh.m
//  qcb_ios
//
//  Created by Ji on 2018/9/3.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "dBCpblJLCFMineAddressAddViewControllerjrpfOh.h"
#import "AreaPickerViewController.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"

@interface dBCpblJLCFMineAddressAddViewControllerjrpfOh ()<UITextFieldDelegate>
{
    NSString *_province;
    NSString *_city;
    NSString *_area;
}

@property (nonatomic, strong) UITextField *eYZLgPnameTextFieldbQpcEE;

@property (nonatomic, strong) UITextField *eHrwEGphoneTextFieldkxdmzK;

@property (nonatomic, strong) UILabel *eFCmLRareaLabelbxLDqx;

@property (nonatomic, strong) UITextView *FjwuIWareaTextViewMgjkCS;

@end

@implementation dBCpblJLCFMineAddressAddViewControllerjrpfOh

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addNavigationBar];
    
    self.navigationBar.title = @"收货地址管理";
    self.view.backgroundColor = QTZ_Gray237;
    [self configView];
    
}

- (void)configView
{
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVBAR_HEIGHT, FULL_WIDTH, 200)];
    bgView.backgroundColor = QTZ_White;
    [self.view addSubview:bgView];
    
    UILabel *LufDjKnameLabelRjatWG = [UILabel labelWithText:@"收货人姓名：" textColor:QTZ_Gray102 font:FONT(13)];
    LufDjKnameLabelRjatWG.left = 13;
    LufDjKnameLabelRjatWG.centerY = 20;
    [bgView addSubview:LufDjKnameLabelRjatWG];
    
    self.eYZLgPnameTextFieldbQpcEE = [[UITextField alloc] initWithFrame:CGRectMake(LufDjKnameLabelRjatWG.right + 20, 0, bgView.width - 21 - LufDjKnameLabelRjatWG.right - 20, 30)];
    self.eYZLgPnameTextFieldbQpcEE.centerY = LufDjKnameLabelRjatWG.centerY;
    self.eYZLgPnameTextFieldbQpcEE.font = FONT(13);
    self.eYZLgPnameTextFieldbQpcEE.returnKeyType = UIReturnKeyDone;
    self.eYZLgPnameTextFieldbQpcEE.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.eYZLgPnameTextFieldbQpcEE.delegate = self;
    [bgView addSubview:self.eYZLgPnameTextFieldbQpcEE];
    [LufDjKnameLabelRjatWG.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, LufDjKnameLabelRjatWG.height + 12) toPoint:CGPointMake(self.eYZLgPnameTextFieldbQpcEE.right, LufDjKnameLabelRjatWG.height + 12) color:QTZ_Gray237 borderWidth:1]];
    
    UILabel *jyvhKwphoneLabelpopXEs = [UILabel labelWithText:@"手机号码：" textColor:QTZ_Gray102 font:FONT(13)];
    jyvhKwphoneLabelpopXEs.left = 13;
    jyvhKwphoneLabelpopXEs.centerY = 60;
    [bgView addSubview:jyvhKwphoneLabelpopXEs];
    
    self.eHrwEGphoneTextFieldkxdmzK = [[UITextField alloc] initWithFrame:CGRectMake(jyvhKwphoneLabelpopXEs.right + 30, 0, bgView.width - 21 - jyvhKwphoneLabelpopXEs.right - 30, 30)];
    self.eHrwEGphoneTextFieldkxdmzK.centerY = jyvhKwphoneLabelpopXEs.centerY;
    self.eHrwEGphoneTextFieldkxdmzK.font = FONT(13);
    self.eHrwEGphoneTextFieldkxdmzK.keyboardType = UIKeyboardTypeNumberPad;
    self.eHrwEGphoneTextFieldkxdmzK.returnKeyType = UIReturnKeyDone;
    self.eHrwEGphoneTextFieldkxdmzK.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.eHrwEGphoneTextFieldkxdmzK.delegate = self;
    [bgView addSubview:self.eHrwEGphoneTextFieldkxdmzK];
    [jyvhKwphoneLabelpopXEs.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, jyvhKwphoneLabelpopXEs.height + 12) toPoint:CGPointMake(self.eHrwEGphoneTextFieldkxdmzK.right, jyvhKwphoneLabelpopXEs.height + 12) color:QTZ_Gray237 borderWidth:1]];
    
    
    UILabel *areaTitleLabel = [UILabel labelWithText:@"所有地区：" textColor:QTZ_Gray102 font:FONT(13)];
    areaTitleLabel.left = 13;
    areaTitleLabel.centerY = 100;
    [bgView addSubview:areaTitleLabel];
    
    
    UIImageView *qTVbTrarrowImgwIPROn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_list_detail_arrow"]];
    qTVbTrarrowImgwIPROn.right = FULL_WIDTH - 25;
    qTVbTrarrowImgwIPROn.centerY = areaTitleLabel.centerY;
    [bgView addSubview:qTVbTrarrowImgwIPROn];
    
    self.eFCmLRareaLabelbxLDqx = [[UILabel alloc] initWithFrame:CGRectMake(areaTitleLabel.right + 30, 0, qTVbTrarrowImgwIPROn.left - 21 - areaTitleLabel.right - 30, 30)];
    self.eFCmLRareaLabelbxLDqx.centerY = areaTitleLabel.centerY;
    self.eFCmLRareaLabelbxLDqx.text = @"请选择";
    self.eFCmLRareaLabelbxLDqx.font = FONT(13);
    self.eFCmLRareaLabelbxLDqx.textColor = QTZ_Gray153;
    self.eFCmLRareaLabelbxLDqx.userInteractionEnabled = YES;
    [self.eFCmLRareaLabelbxLDqx bk_whenTapped:^{
        [self choosePlace];
    }];
    [bgView addSubview:self.eFCmLRareaLabelbxLDqx];
    [areaTitleLabel.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, areaTitleLabel.height + 12) toPoint:CGPointMake(self.eHrwEGphoneTextFieldkxdmzK.right, areaTitleLabel.height + 12) color:QTZ_Gray237 borderWidth:1]];
    
    
    self.FjwuIWareaTextViewMgjkCS = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, FULL_WIDTH - 20, 80)];
    self.FjwuIWareaTextViewMgjkCS.centerY = 160;
    self.FjwuIWareaTextViewMgjkCS.placeholder = @"请填写详细地址，不少于5个字";
    [bgView addSubview:self.FjwuIWareaTextViewMgjkCS ];
    
    
    if (self.address) {
        self.eYZLgPnameTextFieldbQpcEE.text = self.address.toUserName;
        self.eHrwEGphoneTextFieldkxdmzK.text = self.address.phone;
        [self.eFCmLRareaLabelbxLDqx setText:[NSString stringWithFormat:@"%@ %@ %@", self.address.shengfen, self.address.city, self.address.adress]];
        self.FjwuIWareaTextViewMgjkCS.text = self.address.adressDeatil;
    }
    
    
    UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, bgView.bottom + 30, FULL_WIDTH - 40, 40)];
    saveBtn.titleLabel.font = FONT(15);
    [saveBtn setBackgroundColor:Theme_Color];
    [saveBtn setTitle:@"保存" forState:0];
    saveBtn.layer.cornerRadius = 5;
    [saveBtn addTarget:self action:@selector(handleSave) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveBtn];
    
}

- (void)handleSave
{
    if (self.FjwuIWareaTextViewMgjkCS.text.length > 5) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setSafeObject:self.eYZLgPnameTextFieldbQpcEE.text forKey:@"name"];
        [dic setSafeObject:self.eHrwEGphoneTextFieldkxdmzK.text forKey:@"phone"];
        [dic setSafeObject:_province forKey:@"province"];
        [dic setSafeObject:_city forKey:@"city"];
        [dic setSafeObject:_area forKey:@"county"];
        [dic setSafeObject:self.FjwuIWareaTextViewMgjkCS.text forKey:@"address"];
        [dic setSafeObject:self.update ? @"1" : @"0" forKey:@"type"];
        if (self.update) {
            [dic setSafeObject:self.address.id forKey:@"addressId"];
        }
        
        [sHkPtEJLCFMineRequestozAYZs saveAndUpdateAddressWith:dic loadingInView:self.view success:^{
            if (self.update) {
                [self showNotice:@"修改地址成功"];
            } else {
                [self showNotice:@"新增地址成功"];
            }
            [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] popViewControllerAnimated:YES];
            if (self.complete) {
                self.complete();
            }
        } failure:^(NSString *error) {
            [self showNotice:error];
        }];
    } else {
        [self showNotice:@"请填写详细地址，不少于5个字"];
    }
    
    
}

- (void)choosePlace
{
    
    AreaPickerViewController *vc = [AreaPickerViewController new];
    weakify(self);
    vc.handleDoneComplete = ^(NSString *province, NSString *city, NSString *area) {
        strongify(self);
        _province = province;
        _city = city;
        _area = area;
        [self.eFCmLRareaLabelbxLDqx setText:[NSString stringWithFormat:@"%@ %@ %@", province, city, area]];
    };
    self.definesPresentationContext = YES;
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:.5];
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)dZrvUEtextFieldRRHEAtShouldReturn:(UITextField *)dZrvUEtextFieldRRHEAt{
    return [dZrvUEtextFieldRRHEAt resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)dZrvUEtextFieldRRHEAt:(UITextField *)dZrvUEtextFieldRRHEAt shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (dZrvUEtextFieldRRHEAt == self.eHrwEGphoneTextFieldkxdmzK) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.eHrwEGphoneTextFieldkxdmzK.text.length >= 11) {
            self.eHrwEGphoneTextFieldkxdmzK.text = [dZrvUEtextFieldRRHEAt.text substringToIndex:11];
            return NO;
        }
    }
    return YES;
}

- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {
    
    /// 线的路径
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:p0];
    [bezierPath addLineToPoint:p1];
    
    CAShapeLayer * ZLmcEJshapeLayerWDClkp = [CAShapeLayer layer];
    ZLmcEJshapeLayerWDClkp.strokeColor = color.CGColor;
    ZLmcEJshapeLayerWDClkp.fillColor  = [UIColor clearColor].CGColor;
    /// 添加路径
    ZLmcEJshapeLayerWDClkp.path = bezierPath.CGPath;
    /// 线宽度
    ZLmcEJshapeLayerWDClkp.lineWidth = borderWidth;
    return ZLmcEJshapeLayerWDClkp;
}

@end
